package com.yueya.im.business.controller;

import com.yueya.auth.model.Account;
import com.yueya.auth.utils.UserInfoUtil;
import com.yueya.common.base.BaseController;
import com.yueya.common.web.RestResult;
import com.yueya.im.dto.MsgQueryDto;
import com.yueya.im.service.GroupService;
import com.yueya.im.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("${auth.adminPath}/msg")
public class MsgController extends BaseController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private GroupService groupService;
    @RequestMapping("/history")
    public RestResult historyMsg(MsgQueryDto msgQueryDto,int type){
        Account account = UserInfoUtil.getPrincipal().getAccount();
        msgQueryDto.setToId(account.getId());
        if(StringUtils.isBlank(msgQueryDto.getFromId())){
            return RestResult.ERROR("缺少必须参数");
        }
        //查询私聊历史信息
        if(type ==1){
            return messageService.pointMsgPage(msgQueryDto);
        }
        //查询群聊历史信息
        if(type == 2){
            boolean isMember = groupService.isMember(account.getId(),msgQueryDto.getFromId());
            if(isMember){
                return messageService.groupMsgPage(msgQueryDto);
            }else{
                return RestResult.ERROR("非法参数");
            }
        }
        return RestResult.OK("");
    }
}
